<template>
  <el-card>
    <el-form inline>
      <el-form-item label="一级分类">
        <el-select
          v-model="categoryStore.c1Id"
          @change="getC2"
          :disabled="scene === 1"
        >
          <el-option
            :label="c1.name"
            v-for="(c1, index) in categoryStore.c1Arr"
            :key="c1.id"
            :value="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          v-model="categoryStore.c2Id"
          @change="getC3"
          :disabled="scene === 1"
        >
          <el-option
            :label="c2.name"
            v-for="(c2, index) in categoryStore.c2Arr"
            :key="c2.id"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="categoryStore.c3Id" :disabled="scene === 1">
          <el-option
            :label="c3.name"
            v-for="(c3, index) in categoryStore.c3Arr"
            :key="c3.id"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script setup lang="ts">
import {onMounted} from 'vue'
// 引入分类相关的仓库
import useCategoryStore from '@/store/modules/category.ts'

let categoryStore = useCategoryStore()
// 接收父组件的传值
defineProps(['scene'])

// 通知仓库获一级分类的方法
const getC1 = () => {
  categoryStore.getC1()
}
// 一级分类下来菜单change事件
const getC2 = () => {
  // 需要清空二级分类、三级分类的数据
  categoryStore.c2Id = ''
  categoryStore.c3Arr = []
  categoryStore.c3Id = ''
  categoryStore.getC2()
}
const getC3 = () => {
  categoryStore.c3Id = ''
  categoryStore.getC3()
}
// 分类全局组件挂载完毕，通知仓库发请求获取一级分类的数据
onMounted(() => {
  getC1()
})
</script>
<style scoped lang="scss"></style>
